package com.hhsj.glidesource.javaDesign.sigleTone;

public class Test {


    public static void main(String[] args) {
        SingleTone.getInstance();//饿汉模式
        SingleTone2.getInstance();//懒汉模式
    }

}


class SingleTone {
    //线程安全
    private static SingleTone INSTANCE = new SingleTone();

    private SingleTone() {
        System.out.print("私有构造");
    }

    public static SingleTone getInstance() {
        return INSTANCE;
    }
}


class SingleTone2 {

    //非线程安全
    private static volatile SingleTone2 INSTANCE;

    private SingleTone2() {
        System.out.print("私有构造");
    }

    public static SingleTone2 getInstance() {
        if (null == INSTANCE) {
            synchronized (SingleTone2.class) {
                if (null == INSTANCE) {
                    INSTANCE = new SingleTone2();
                }
            }
        }
        return INSTANCE;
    }

}